﻿/*
    Copyright (c) Christian Ranz 2009
    http://www.christian-ranz.com
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LogFileConnector
{
    class LogConnector
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public bool connectLogs(List<string> files, string destination)
        {
            List<string> fileContent = new List<string>();
            StreamWriter writer = new StreamWriter(destination, Configuration.destinationAppend, Configuration.destinationEncoding);;
            try
            {
                LogValidator validator = new LogValidator();
                foreach (string file in files)
                {
                    try
                    {
                        if (validator.isValid(file))
                        {
                            fileContent.AddRange(validator.getFileContent(file));
                        }
                    }
                    catch (Exception validError)
                    {
                        writer.Close();
                        throw new Exception("Fehler beim öffnen der Datei " + file + " . Details: " + validError.Message);
                    }
                    finally
                    {
                        try
                        {
                            if (Configuration.deleteSourceFiles) {
                                System.IO.File.Delete(file);
                                ErrorHandler.processInfoOutput("Die Datei " + file + " wurde erfolgreich gelöscht.");
                            }
                        }
                        catch (Exception delError)
                        {
                            ErrorHandler.writeLog("The Datei " + file + " konnte nicht gelöscht werden. Grund: "+delError.Message);
                        }
                    }
                }


                foreach (string contentLine in fileContent)
                {
                    writer.WriteLine(contentLine);
                }
                writer.Close();
            }
            catch (Exception openError)
            {
                throw new Exception("Beim Zugriff auf die Datei " + destination + " ist ein Fehler aufgetreten. Details: " + openError.Message);
            }
            finally
            {
                writer.Close();
            }
            return true;
        }
    }
}
